package cn.com.fuhuitong.main.cate.adapter

import android.content.Context
import android.graphics.Typeface
import android.view.View
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.cate.entity.CateChildEntity
import cn.com.fuhuitong.main.cate.entity.CateGroupEntity
import com.alibaba.android.vlayout.LayoutHelper
import com.bumptech.glide.Glide
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.getImageUrl
import kotlinx.android.synthetic.main.item_category_child.view.*
import kotlinx.android.synthetic.main.item_category_group.view.*

enum class CVT(value: Int) {
    CATEGORY_GROUP(1),
    CATEGORY_CHILD(2)
}

class CategoryGroupAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<CateGroupEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_category_group,
    mItemViewType = CVT.CATEGORY_GROUP.ordinal
) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.itemView.text_category_group_name.text = data[position].name
        holder.itemView.text_category_group_name.typeface = if (selectedPosition == position)
            Typeface.defaultFromStyle(Typeface.BOLD) else Typeface.defaultFromStyle(Typeface.NORMAL)
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class CategoryChildAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<CateChildEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_category_child,
    mItemViewType = CVT.CATEGORY_CHILD.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int) {
        super.onBindViewHolder(itemView, position)
        Glide.with(itemView).load(getImageUrl(data[position].image))
            .into(itemView.image_category_icon)
        itemView.text_category_name.text = data[position].name
    }
}